<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>平方练习</title>
</head>
<body>
    <input type="text">
    <input type="button" value="平方" onclick="f()">
    <h2>结果:<span></span></h2>
    <script>
        function f(){
            // 获取文本框
            let i = document.querySelector("input")
            // 获取span的值
            let s = document.querySelector("span")
            // 判断输入的值是否为数字,如果不是打印输入错误.
            if(isNaN(i.value)){
                s.innerText = "输入错误"
                return
            }
            // 将文本框中的值赋给span
            s.innerText = i.value*i.value;
            // 加法
            // s.innerText = i.value*1+i.value*1;
        }
    </script>
</body>
</html>